<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>执行 cURL 会话</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.curl-escape.html">« curl_escape</a></li>
      <li style="float: right;"><a href="function.curl-getinfo.html">curl_getinfo »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.curl.html">cURL 函数</a></li>
    <li>执行 cURL 会话</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.curl-exec" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">curl_exec</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.0.2, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">curl_exec</span> &mdash; <span class="dc-title">执行 cURL 会话</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.curl-exec-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>curl_exec</strong></span>(<span class="methodparam"><span class="type">resource</span> <code class="parameter">$ch</code></span>): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>

  <p class="para rdfs-comment">
   执行给定的 cURL 会话。
  </p>
  <p class="para">
   这个函数应该在初始化一个 cURL 会话并且全部的选项都被设置后被调用。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.curl-exec-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    <dt>
<code class="parameter">handle</code>
</dt>
<dd>
<p class="para">由 <span class="function"><a href="function.curl-init.html" class="function">curl_init()</a></span> 返回的 cURL 句柄。</p></dd>

   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.curl-exec-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>true</code></strong>， 或者在失败时返回 <strong><code>false</code></strong>。 然而，如果 <a href="function.curl-setopt.html" class="link">设置</a>了  <strong><code>CURLOPT_RETURNTRANSFER</code></strong> 选项，函数执行成功时会返回执行的结果，失败时返回 <strong><code>false</code></strong> 。
  </p>
  <div class="warning"><strong class="warning">警告</strong><p class="simpara">此函数可能返回布尔值
<strong><code>false</code></strong>，但也可能返回等同于 <strong><code>false</code></strong> 的非布尔值。请阅读 <a href="language.types.boolean.html" class="link">布尔类型</a>章节以获取更多信息。应使用
<a href="language.operators.comparison.html" class="link">===
运算符</a>来测试此函数的返回值。</p></div>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    注意：响应代码是错误码时（例如<code class="literal">404
    Not found</code>）也不会被当作失败。这种情况可以使用 <span class="function"><a href="function.curl-getinfo.html" class="function">curl_getinfo()</a></span> 来检查。
   </p>
  </p></blockquote>
 </div>

   
 <div class="refsect1 examples" id="refsect1-function.curl-exec-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4196">
    <p><strong>示例 #1 获取网页</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;创建新的&nbsp;cURL&nbsp;资源<br /></span><span style="color: #0000BB">$ch&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">curl_init</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">//&nbsp;设置&nbsp;URL&nbsp;和相应的选项<br /></span><span style="color: #0000BB">curl_setopt</span><span style="color: #007700">(</span><span style="color: #0000BB">$ch</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">CURLOPT_URL</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"http://www.example.com/"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">curl_setopt</span><span style="color: #007700">(</span><span style="color: #0000BB">$ch</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">CURLOPT_HEADER</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;抓取&nbsp;URL&nbsp;并把它传递给浏览器<br /></span><span style="color: #0000BB">curl_exec</span><span style="color: #007700">(</span><span style="color: #0000BB">$ch</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;关闭&nbsp;cURL&nbsp;资源，并且释放系统资源<br /></span><span style="color: #0000BB">curl_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$ch</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>

 
 <div class="refsect1 seealso" id="refsect1-function.curl-exec-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.curl-multi-exec.html" class="function" rel="rdfs-seeAlso">curl_multi_exec()</a> - 运行当前 cURL 句柄的子连接</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>